package tc.de.krawallmann.twl.widgets;

import tc.de.krawallmann.event.OKDialogListener;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.DesktopArea;

/**
 * The Class OKDialog.
 * @author KrawallMann
 * @version 1.0
 */
public class OKDialog extends DesktopArea {

	/** The action. */
	private transient final String action;

	/** The button. */
	private transient final Button button;

	/** The label. */
	private transient final MulticolorLabel label;

	/** The message. */
	private transient final String message;

	/**
	 * Instantiates a new oK dialog.
	 * 
	 * @param message
	 *            the message
	 * @param action
	 *            the action
	 * @param listener
	 *            the listener
	 */
	public OKDialog(final String message, final String action, final OKDialogListener listener) {
		super();
		setTheme("borderedwidget");
		this.message = message;
		this.action = action;
		label = new MulticolorLabel(message);
		label.setTheme("/label40");
		button = new Button(action);
		button.setTheme("/button40");
		button.addCallback(new Runnable() {
			@Override
			public void run() {
				listener.okPressed();
				setVisible(false);
			}
		});
		add(label);
		add(button);
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.Widget#adjustSize()
	 */
	@Override
	public void adjustSize() {
		super.adjustSize();
		button.adjustSize();
		label.adjustSize();
		setSize(Math.max(Math.max(400, button.getWidth()), label.getWidth()), 200);
	}

	/**
	 * Gets the action.
	 * 
	 * @return the action
	 */
	public String getAction() {
		return action;
	}

	/**
	 * Gets the message.
	 * 
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/*
	 * (non-Javadoc)
	 * @see de.matthiasmann.twl.DesktopArea#layout()
	 */
	@Override
	public void layout() {
		super.layout();
		label.adjustSize();
		label.setPosition(getX() + (getWidth() - label.getWidth()) / 2, getY() + 20);
		button.adjustSize();
		button.setPosition(getX() + (getWidth() - button.getWidth()) / 2, getY() + 120);
	}

	/**
	 * Sets the button text.
	 * 
	 * @param buttonText
	 *            the new button text
	 */
	public void setButtonText(final String buttonText) {
		button.setText(buttonText);
		layout();
	}

	public void setColor(final MulticolorLabel.Color color) {
		label.setColor(color);
	}

	/**
	 * Sets the label text.
	 * 
	 * @param labelText
	 *            the new label text
	 */
	public void setLabelText(final String labelText) {
		label.setText(labelText);
		layout();
	}

}
